<?php
namespace MLM\Controller;

use Zend\View\Model\ViewModel;
use Zend\Authentication\Result;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as AuthAdapter;

class JsondataController extends \MLM\Model\Generic\Controller
{
    
    

    public function indexAction()
    {
        //
    }


    public function gensnAction()
    {

        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $num = (isset($getParams['num'])) ? $getParams['num'] : "";
        $tmp = array();

        $db = $this->_db;
        $conn = $db->getDriver()->getConnection();
        
        if ($num != ""){
            $limit = $this->param["rows"];
            $start = ($this->param["page"]-1)*$limit;
            
            $storage = new \MLM\Model\Sn\Storage($db);
            $getPrefix = $storage->getSNConfig('SN_PREFIX');
            while($getPrefix->next()){
                    $res = $getPrefix->current();
                    $prefix = $res['str_value'];
            }


            $getLen = $storage->getSNConfig('SN_NUMBER_LENGTH');
            while($getLen->next()){
                    $res = $getLen->current();
                    $len = $res['int_value'];
            }

            $dataSN = array();
            $cnt = 0;

            $conn->beginTransaction();

            for ($i=0;$i<$num;$i++){
                $res = $storage->genSN($prefix, $len);
                $res = explode(",", $res);
                array_push($dataSN, array("SN" => $res[0], "PIN" => $res[1]));
                $cnt++;
            }

            $conn->commit();
            
            $data = array($cnt);
            array_push($data, $dataSN);
            
            $tmp['total'] = $data[0];
            $tmp['rows'] = $data[1];
            
        }
        

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($tmp));
        return $view;

    }

    public function cetaksnAction()
    {
        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $rows = $getParams["rows"];
        $page = $getParams["page"];

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }

        $limit = $rows;
        $start = ($page-1)*$limit;

        $storage = new \MLM\Model\Sn\Storage($this->_db);
        $data = $storage->getSN(1, "", "", $start, $limit, $search, $searchField);

               
        $total = $data[0];
        $rows = $data[1];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0; 
        }

        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));
        return $view;

    }


    public function getsnAction()
    {
        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $rows = $getParams["rows"];
        $page = $getParams["page"];

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }
            
        }

        
        
        $limit = $rows;
        $start = ($page-1)*$limit;

        $sn = (isset($getParams['sn'])) ? $getParams['sn'] : "";
        $stts = (isset($getParams['stts'])) ? $getParams['stts'] : "";


        $storage = new \MLM\Model\Sn\Storage($this->_db);
        $data = $storage->getSN("", $sn, $stts, $start, $limit, $search, $searchField);

        $total = $data[0];
        $rows = $data[1];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }

        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));
        return $view;

        
    }


    public function getagentsnAction()
    {
        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $type = ($getParams["type"] == 2) ? "MEMBER" : "AGENT" ;

        $search = (isset($getParams["search"])) ? $getParams["search"] : $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }


        $limit = $rows;
        $start = ($page-1)*$limit;

        $sn = (isset($getParams['sn'])) ? $getParams['sn'] : "";
        $stts = (isset($getParams['stts'])) ? $getParams['stts'] : "";


        $storage = new \MLM\Model\Sn\Storage($this->_db);
        $data = $storage->getSN("", $sn, $stts, $start, $limit, $search, $searchField, $type);

        $total = $data[0];
        $rows = $data[1];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }

        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));
        return $view;


    }


    public function trackingtransAction()
    {
        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();
        

        $transType = (isset($getParams['transType'])) ? $getParams['transType'] : "";
        $date1 = (isset($getParams['date1'])) ? str_replace('-', '', $getParams['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($getParams['date2'])) ? str_replace('-', '', $getParams['date2']) : date('Ymd');
        $mutation = $getParams["mutation"];

        
        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }

        $accNum = $identity->member_code;
        $storage = new \MLM\Model\Transaction\Storage($this->_db);
        $data = $storage->getTrackingTransaction($accNum, $transType, $date1, $date2, $start, $limit, $searchField, $mutation);

        $total = $data[0];
        $rows = $data[2];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }

    public function mutasitransadminAction()
    {
        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();


        $transType = (isset($getParams['transType'])) ? $getParams['transType'] : "";
        $accNum = (isset($getParams['accNum'])) ? $getParams['accNum'] : "";
        $date1 = (isset($getParams['date1'])) ? str_replace('-', '', $getParams['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($getParams['date2'])) ? str_replace('-', '', $getParams['date2']) : date('Ymd');


        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }

        $storage = new \MLM\Model\Transaction\Storage($this->_db);
        $data = $storage->getTrackingTransaction($accNum, $transType, $date1, $date2, $start, $limit, $searchField);

        $total = $data[0];
        $rows = $data[2];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }


    public function mutasitransmonthadminAction()
    {
        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();


        $transType = (isset($getParams['transType'])) ? $getParams['transType'] : "";
        $accNum = (isset($getParams['accNum'])) ? $getParams['accNum'] : "";
        $month = (isset($getParams['month'])) ? str_replace('-', '', $getParams['month']) : date('Ym', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));


        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }

        $storage = new \MLM\Model\Transaction\Storage($this->_db);
        $data = $storage->getTrackingTransactionMonthly($accNum, $transType, $month, $start, $limit, $searchField);

        $total = $data[0];
        $rows = $data[2];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }


    public function saldoadminAction()
    {
        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();


        $accNum = (isset($getParams['accNum'])) ? $getParams['accNum'] : "";
        
        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }

        $storage = new \MLM\Model\Report\Storage($this->_db);
        $data = $storage->getSaldoMembers($accNum, $start, $limit, $searchField);

        $total = $data[0];
        $rows = $data[2];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }

    public function getproductsAction(){
        $identity = $this->hasSession();
        
        $getParams = $this->getRequest()->getQuery();
        $suplierId = $getParams["slSuplier"];
        $agentId = $getParams["agent"];

        $storage = new \MLM\Model\Agent\Storage($this->_db);
        $products = $storage->getProductBySuplier($suplierId, $agentId);
        
        $prod = array();
        while($products->next()){
                $res = $products->current();
                $len = $res['int_value'];
                $prod[] = array("product_id" => $res["product_id"], 
                                "product_name" => $res["product_name"],
                                "sale_price" => $res["sale_price"],
                                "basic_price" => $res["basic_price"], 
                                "agent_price" => $res["agent_price"], 
                                "member_price" => $res["agent2_price"],
                                "qty" => $res["qty"]);
        }

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($prod));
        return $view;
    }


    public function buytransAction()
    {
        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();
        
        $transType = (isset($getParams['transType'])) ? $getParams['transType'] : "";
        $transCode = (isset($getParams['transCode'])) ? $getParams['transCode'] : "";
        $admin = (isset($getParams['admin'])) ? $getParams['admin'] : "";
        $date1 = (isset($getParams['date1']) && $getParams['date1'] != "") ? str_replace('-', '', $getParams['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($getParams['date2']) && $getParams['date2'] != "") ? str_replace('-', '', $getParams['date2']) : date('Ymd');


        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }

        
        $accNum = $identity->member_code;
        $storage = new \MLM\Model\Transaction\Storage($this->_db);
        $data = $storage->getBuyTransaction($accNum, $transCode, $date1, $date2, $start, $limit, $searchField);

        $total = $data[0];
        $rows = $data[1];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }


    public function confirmlistAction()
    {
        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $transType = (isset($getParams['transType'])) ? $getParams['transType'] : "";
        $transCode = (isset($getParams['transCode'])) ? $getParams['transCode'] : "";
        $admin = (isset($getParams['admin'])) ? $getParams['admin'] : "";
        $date1 = (isset($getParams['date1']) && $getParams['date1'] != "") ? str_replace('-', '', $getParams['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($getParams['date2']) && $getParams['date2'] != "") ? str_replace('-', '', $getParams['date2']) : date('Ymd');


        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];
        
        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }


        $storage = new \MLM\Model\Transaction\Storage($this->_db);
        $data = $storage->getConfirmPayment($identity->member_type_id, $identity->member_id, $transCode, $date1, $date2, $start, $limit, $searchField);

        $total = $data[0];
        $rows = $data[1];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }

    public function withdrawalistAction(){

        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];
        
        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }


        $storage = new \MLM\Model\Transaction\Storage($this->_db);        
        $data = $storage->getWithdrawList($start, $limit, $searchField);

        $total = $data[0];
        $rows = $data[2];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;
        
    }


    public function topuplistAction(){

        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }

        $storage = new \MLM\Model\Transaction\Storage($this->_db);
        $data = $storage->getTopupList($start, $limit, $searchField);

        $total = $data[0];
        $rows = $data[2];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }



    public function buysnlistAction(){

        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $transCode = $getParams["transCode"];
        $date1 = (isset($getParams['date1'])) ? str_replace('-', '', $getParams['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($getParams['date2'])) ? str_replace('-', '', $getParams['date2']) : date('Ymd');

        
        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }

        $memberType = $identity->member_type_id;
        $memberCode = $identity->member_code;

        $storage = new \MLM\Model\Transaction\Storage($this->_db);
        $data = $storage->getBuySNList($transCode, $date1, $date2, $start, $limit, $searchField, $memberType, $memberCode);

        $total = $data[0];
        $rows = $data[2];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }


    public function buyproductlistAction(){

        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $transCode = $getParams["transCode"];
        $date1 = (isset($getParams['date1'])) ? str_replace('-', '', $getParams['date1']) : "";
        $date2 = (isset($getParams['date2'])) ? str_replace('-', '', $getParams['date2']) : "";
        
        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }

        /*
        if ($identity->member_id == 100) $memberId = $identity->member_id;
        else $memberId = $identity->member_reff_id;
         * 
         */

        $memberId = $identity->member_id;
        $memberType = $identity->member_type_id;
        $memberCode = $identity->member_code;
		
		$approvalId = $identity->member_reff_id;

        $storage = new \MLM\Model\Transaction\Storage($this->_db);
        $data = $storage->getBuyProductList($memberId, $transCode, $date1, $date2, $start, $limit, $searchField, $memberType, $memberCode);

        $total = $data[0];
        $rows = $data[1];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }


    public function addagentlistAction(){

        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }


        $storage = new \MLM\Model\Transaction\Storage($this->_db);
        $data = $storage->getAgentList($start, $limit, $searchField);

        $total = $data[0];
        $rows = $data[2];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }


    public function agentlistAction(){

        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $agentCode = $getParams["agentCode"];
        $agentName = $getParams["agentName"];
        $addr = $getParams["addr"];

        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }


        $storage = new \MLM\Model\Member\Storage($this->_db);
        $data = $storage->getAgentList($agentCode, $agentName, $addr, $start, $limit, $searchField);

        $total = $data[0];
        $rows = $data[1];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }


    public function memberlistAction(){

        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $code = $getParams["code"];
        $name = $getParams["name"];
        $addr = $getParams["addr"];
        $regdate = (isset($getParams['regdate'])) ? str_replace('-', '', $getParams['regdate']) : "";

        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }


        $storage = new \MLM\Model\Member\Storage($this->_db);
        $data = $storage->getMemberList($code, $name, $addr, $start, $limit, $searchField, $regdate);

        $total = $data[0];
        $rows = $data[1];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }


    public function matrixAction(){

        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $lvl = $getParams["level"];
        $code = $getParams["code"];
        $name = $getParams["name"];

        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }

        $memberId = $identity->member_id;
        $storage = new \MLM\Model\Member\Storage($this->_db);
        $data = $storage->getMemberMatrix($memberId, $lvl, $code, $name, $start, $limit, $searchField);

        $total = $data[0];
        $rows = $data[1];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }


    public function claimrewardlistAction(){

        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }


        $storage = new \MLM\Model\Transaction\Storage($this->_db);
        $data = $storage->getClaimRewardList($start, $limit, $searchField);

        $total = $data[0];
        $rows = $data[2];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }

    public function transdetailproductAction(){

        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $transId = $getParams["transId"];

        $storage = new \MLM\Model\Transaction\Storage($this->_db);
        $data = $storage->getTransDetailProduct($transId);

        $total = $data[0];
        $rows = $data[1];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		
        $response->page = 1;
        $response->total = 1;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }
    

    public function transdetailAction()
    {
        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $transId = $getParams["transid"];

        $storage = new \MLM\Model\Transaction\Storage($this->_db);

        $data = $storage->getTransDetail($transId);
        $ph = array();
        while($data->next()){
                $res = $data->current();
                $ph = array("account_num1" => $res["account_num1"],
                            "account_num2" => $res["account_num2"],
                            "trans_dtm" => $res["trans_dtm"],
                            "trans_type_name" => $res["trans_type_name"],
                            "amount" => number_format($res["amount1"]),
                            "mutation_type" => $res["mutation_type"],
                            "notes_txt" => $res["notes_txt"],
                            "trans_status" => $res["trans_status"],
                            "account_name" => $res["account_name"],
                            "xs1" => $res["xs1"],
                            "xs2" => $res["xs2"],
                            "xs3" => $res["xs3"],
                            "xs4" => $res["xs4"],
                            "xs5" => $res["xs5"],
                            "xs6" => $res["xs6"],
                            "bank_acc_no" => $res["bank_acc_no"],
                            "bank_acc_name" => $res["bank_acc_name"]);
        }


        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($ph));
        return $view;

    }


    public function membergrowthAction()
    {
        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();


        $date1 = (isset($getParams['date1'])) ? str_replace('-', '', $getParams['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($getParams['date2'])) ? str_replace('-', '', $getParams['date2']) : date('Ymd');


        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }

        $storage = new \MLM\Model\Member\Storage($this->_db);
        $data = $storage->getPerkembanganMember($date1, $date2, $start, $limit, $searchField);

        $total = $data[0];
        $rows = $data[1];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }

    public function productlistAction(){

        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $act = (isset($getParams["act"])) ? $getParams["act"] : "";
        $prodCode = $getParams["product_code"];
        $suplier = $getParams["suplier_id"];
        $date1 = (isset($getParams['start_dat'])) ? str_replace('-', '', $getParams['start_dat']) : "" ;
        $date2 = (isset($getParams['end_dat'])) ? str_replace('-', '', $getParams['end_dat']) : "";

        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }


        $storage = new \MLM\Model\Product\Storage($this->_db);

        //Delete Produk
        if($act == "del"){
            $db = $this->_db;
            $conn = $db->getDriver()->getConnection();
            $conn->beginTransaction();

            $prodId = $getParams["prodId"];
            $storage->delProduct($prodId);

            $conn->commit();
        }

        
        $data = $storage->getProductList($prodCode, $suplier, $date1, $date2, $start, $limit, $searchField);

        $total = $data[0];
        $rows = $data[2];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }


    public function productpricelistAction(){

        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $act = (isset($getParams["act"])) ? $getParams["act"] : "";
        $prodId = $getParams["prodId"];
        
        
        //$date1 = (isset($getParams['start_dat'])) ? str_replace('-', '', $getParams['start_dat']) : "" ;
        //$date2 = (isset($getParams['end_dat'])) ? str_replace('-', '', $getParams['end_dat']) : "";

        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }

        $db = $this->_db;
        $conn = $db->getDriver()->getConnection();
        
        $priceStorage = new \MLM\Model\Product\Price\Storage($db);
        $price = new \MLM\Model\Product\Price($priceStorage);

        //Delete Produk
        if($act == "del"){
            $prodPriceId = $getParams["prodPriceId"];

            $conn->beginTransaction();
            $priceStorage->del($prodPriceId);
            $conn->commit();

        }else if($act == "add"){
            
            $conn->beginTransaction();

            $price->productId = $getParams["prodId"];
            $price->basicPrice = $getParams["price1"];
            $price->salePrice = $getParams["price2"];
            $price->agentPrice = $getParams["price3"];
            $price->memberPrice = $getParams["price4"];
            $price->startDat = $getParams["date1"];

            if ($getParams["date2"] != "") $price->endDat = $getParams["date2"];

            $priceId = $price->save();

            if (!$priceId) $conn->rollBack();
            
            $conn->commit();

        }


        $data = $priceStorage->getProductPriceList($prodId, $searchField);
        $total = $data[0];
        $rows = $data[1];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }


    public function productstocklistAction(){

        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $act = (isset($getParams["act"])) ? $getParams["act"] : "";
        $memberId = $getParams["memberId"];
        $prodId = $getParams["prodId"];
        
        $view = new ViewModel();

        
        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }

        $db = $this->_db;
        $conn = $db->getDriver()->getConnection();

        $stockStorage = new \MLM\Model\Product\Stock\Storage($db);
        $stock = new \MLM\Model\Product\Stock($stockStorage);

        //Delete Produk
        if($act == "del"){
            $prodStockId = $getParams["prodStockId"];

            $conn->beginTransaction();
            $stockStorage->del($prodStockId);
            $conn->commit();

        }else if($act == "add"){

            $conn->beginTransaction();

            //Cek stok
            $cekStock = $stockStorage->getMemberStock($prodId, $memberId);
            $cnt = 0;
            while($cekStock->next()){
                $res = $cekStock->current();
                $cnt++;
            }


            if ($cnt == 0){
                $stock->productId = $prodId;
                $stock->memberId = $memberId;
                $stock->qty = $getParams["qty"];

                $stockId = $stock->save();

                if (!$stockId) $conn->rollBack();
            }else{

                //Update stok produk
                $productId = $stockStorage->updateStock($prodId, $memberId, '+', $getParams["qty"]);

                if(!$productId){
                    $conn->rollBack();
                }
            }

            $conn->commit();

        }


        $data = $stockStorage->getProductStockList($prodId, $memberId, $searchField, $start, $limit);
        $total = $data[0];
        $rows = $data[1];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }


    public function infoproductpriceAction(){

        $identity = $this->hasSession();

        $isAgent = $identity->member_reff_id;

        $view = new ViewModel();

        $getParams = $this->getRequest()->getQuery();
        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }

        $priceStorage = new \MLM\Model\Product\Price\Storage($this->_db);
        $data = $priceStorage->getInfoProductPrice($isAgent, $searchField, $start, $limit);
        $total = $data[0];
        $rows = $data[1];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }

    public function rewardlistAction(){

        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }

        $numMitra = 0;
        /*
        $memberStorage = new \MLM\Model\Member\Storage($this->_db);
        $getNumMitra = $memberStorage->getNumMitra($identity->member_id);
        while($getNumMitra->next()){
                $res = $getNumMitra->current();
                $numMitra = $res['total'];
        }
         * 
         */
        
        $storage = new \MLM\Model\Transaction\Storage($this->_db);        
        $data = $storage->getRewardList($identity->member_id, $numMitra, $start, $limit, $searchField);

        $total = $data[0];
        $rows = $data[1];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }


    public function rewardsnlistAction(){

        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }

        $numSN = 0;
        
        $snStorage = new \MLM\Model\Sn\Storage($this->_db);
        $getNumSN = $snStorage->getNumSNMitra($identity->member_id);
        while($getNumSN->next()){
                $res = $getNumSN->current();
                $numSN = $res['total'];
        }
        

        $storage = new \MLM\Model\Transaction\Storage($this->_db);
        $data = $storage->getRewardSNList($identity->member_id, $numSN, $start, $limit, $searchField);

        $total = $data[0];
        $rows = $data[1];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }


    public function configlistAction(){

        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $act = (isset($getParams["act"])) ? $getParams["act"] : "";
        $configName = $getParams["configName"];
        
        $rows = $getParams["rows"];
        $page = $getParams["page"];
        $limit = $rows;
        $start = ($page-1)*$limit;

        $search = $getParams["_search"];

        if($search == 'true'){
            $searchField = $getParams["searchField"];
            $searchOper = $getParams["searchOper"];
            $searchString = $getParams["searchString"];

            if ($searchOper == 'eq'){
                $searchField .= " = '$searchString' ";
            }elseif ($searchOper == 'ne'){
                $searchField .= " <> '$searchString' ";
            }elseif ($searchOper == 'bw'){
                $searchField .= " like '$searchString%' ";
            }elseif ($searchOper == 'bn'){
                $searchField .= " not like '%$searchString' ";
            }elseif ($searchOper == 'ew'){
                $searchField .= " like '%$searchString' ";
            }elseif ($searchOper == 'en'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'cn'){
                $searchField .= " like '%$searchString%' ";
            }elseif ($searchOper == 'nc'){
                $searchField .= " not like '%$searchString%' ";
            }elseif ($searchOper == 'in'){
                $searchField .= " in ($searchString) ";
            }elseif ($searchOper == 'ni'){
                $searchField .= " not in ($searchString) ";
            }

        }


        $storage = new \MLM\Model\Sysconfig\Storage($this->_db);

        //Delete Config
        if($act == "del"){
            $db = $this->_db;
            $conn = $db->getDriver()->getConnection();
            $conn->beginTransaction();
            
            $configId = $getParams["configId"];
            $storage->delConfig($configId);

            $conn->commit();
        }


        $data = $storage->getConfigList($configName, $start, $limit, $searchField);

        $total = $data[0];
        $rows = $data[2];

        if( $total > 0 ) {
            $total_pages = ceil($total/$limit);
        } else {
            $total_pages = 0;
        }
		//$response = new stdClass();
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $total;
        $response->rows = $rows;

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($response));

        return $view;

    }


    public function sponsorgridAction()
    {
        $identity = $this->hasSession();
        $getParams = $this->getRequest()->getQuery();
        $memberId = (isset($getParams['memberId'])) ? $getParams['memberId'] : $identity->member_id;
        
        $storage = new \MLM\Model\Network\Storage($this->_db);

        $result = $storage->getSponsor($memberId);

        //$rows = array();
        $num = 0;

        /*
        while($result->next()){
                $res = $result->current();
                $num++;
                
                array_push($rows, array("id" => $res['id'],
                                             "sn" => $res['member_code'],
                                             "name" => $res['member_name'],
                                             "alamat" => $res['address']));
        }
         * 
         */


        $tmp = "<?xml version='1.0' encoding='utf-8'?>\n";
        $tmp .= "<rows>";
        $tmp .= "<page>1</page>";
        $tmp .= "<total>1</total>";
        $tmp .= "<records>1</records>";
        //// be sure to put text data in CDATA
        while($result->next()){
                $row = $result->current();
                $num++;
            $tmp .= "<row>";
            $tmp .= "<cell>". $row['id']."</cell>";
            $tmp .= "<cell>". $row['member_code']."</cell>";
            $tmp .= "<cell>". $row['member_name']."</cell>";
            $tmp .= "<cell>". $row['address']."</cell>";
            $tmp .= "<cell>". $row['level']."0</cell>";
            $tmp .= "<cell>". $row['lft']."1</cell>";
            $tmp .= "<cell>". $row['rgt']."8</cell>";
            if($row['rgt'] == $row['lft']+1) $leaf = 'true';
            else $leaf='false';
            $tmp .= "<cell>".$leaf."</cell>";
            $tmp .= "<cell>false</cell>";
            $tmp .= "</row>";

        }
        $tmp .= "</rows>";

        $view = new ViewModel();
        $view->setTerminal(true);
        //$view->setVariable('data', json_encode($response));

        $view->setVariable('data', $tmp);

        return $view;

    }


    public function getagentsnstockAction(){
        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();
        $agentId = $getParams["agent"];

        $storage = new \MLM\Model\Agent\Storage($this->_db);
        $products = $storage->getSNStockByAgent($agentId);

        $qty = 0;
        while($products->next()){
                $res = $products->current();                
                $qty = $res["total"];
        }

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', $qty);
        return $view;
    }
    

    public function vouchercodeAction()
    {
        $identity = $this->hasSession();

        $getParams = $this->getRequest()->getQuery();

        $slOperator = (isset($getParams['slOperator'])) ? $getParams['slOperator'] : "";

        $facStorage = new \MLM\Model\Facility\Storage($this->_db);

        
        $data = $facStorage->getVoucherCode($slOperator);
        $voucher = array();
        while($data->next()){
                $res = $data->current();
                $len = $res['int_value'];
                $voucher[] = array("p_pulsa_id" => $res["p_pulsa_id"],
                                "pulsa_code" => $res["pulsa_code"],
                                "pulsa_desc" => $res["pulsa_desc"],
                                "pulsa_operator" => $res["pulsa_operator"],
                                "nominal" => $res["nominal"],
                                "price" => $res["price"]
                                );
        }

        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($voucher));
        return $view;

    }


    public function daftarpribadiAction()
    {
        $identity = $this->hasSession();
        $memberId   = $identity->member_id;

        $getParams = $this->getRequest()->getQuery();
        
        $payFor = (isset($getParams['pay_for'])) ? $getParams['pay_for'] : "";

        $modelAcc = new \MLM\Model\Member\Account\Storage($this->_db);
        
        $data = $modelAcc->getPersonalAccByTransCode($memberId, $payFor);
        $ph = array();
        while($data->next()){
                $res = $data->current();
                $ph[] = array("phone_num" => $res["phone_num"],
                                "trans_name" => $res["trans_name"]
                                );
        }
        
        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($ph));
        return $view;

    }


    public function getaccountAction()
    {
        $identity = $this->hasSession();
        
        $getParams = $this->getRequest()->getQuery();
        
        $memberId = (isset($getParams['memberid'])) ? $getParams['memberid'] : "";

        $modelAcc = new \MLM\Model\Member\Account\Storage($this->_db);

        $data = $modelAcc->getAccountBank($memberId);
        $ph = array();
        while($data->next()){
                $res = $data->current();
                $ph = array("account_num" => $res["account_num"],
                                "account_name" => $res["account_name"],
                                "account_status" => $res["account_status"],
                                "bank_acc_no" => $res["bank_acc_no"],
                                "bank_acc_name" => $res["bank_acc_name"],
                                "bank_acc_name" => $res["bank_acc_name"],
                                "amount" => number_format($res["amount"])
                                );
        }



        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', json_encode($ph));        
        return $view;

    }

    
    private function array_to_xml($student_info, &$xml_student_info) {
        foreach($student_info as $key => $value) {
            if(is_array($value)) {
                if(!is_numeric($key)){
                    $subnode = $xml_student_info->addChild("$key");
                    array_to_xml($value, $subnode);
                }
                else{
                    $subnode = $xml_student_info->addChild("item$key");
                    array_to_xml($value, $subnode);
                }
            }
            else {
                $xml_student_info->addChild("$key",htmlspecialchars("$value"));
            }
        }
    }
    
    private function hasSession() {
        $auth = new AuthenticationService();

        if (!$auth->hasIdentity()) {
            return $this->redirect()->toRoute('application/default', array('controller' => 'Index', 'action' => 'login'));
        } else {
            return $auth->getIdentity();
        }
    }
    
}

